package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.VrGoodsDataClass;
import com.limao.DBProvider.VrGoodsProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class VrGoodsCache  extends BasicModule{

	private VrGoodsProvider provider;
	
	private static VrGoodsCache instance;

    public static VrGoodsCache getInstance() {
        return instance;
    }
    
    private Cache<String, VrGoodsDataClass> vrGoodsCache;

    public VrGoodsCache() {
    	super("VrGoodsCache");
    	vrGoodsCache = CacheFactory.createCache("vr_GoodsCache");
        initProvider();
    }
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    
    public void ClearObjectCache()
    {
    	vrGoodsCache.clear();
    }   
    
    //**************************************************************************
 
    public List<VrGoodsDataClass> Load_All_VrGoodsList()
    {
    	List<VrGoodsDataClass> list =new ArrayList<VrGoodsDataClass>();
    	if(vrGoodsCache.size()==0)
    	{
    		list=provider.Load_All_VrGoodsList();
    		if(list.size()>0)
    		{
    			for(VrGoodsDataClass obj:list)
    			{
    				vrGoodsCache.put(obj.vrgoodsid, obj);
    			}
    		}
    	}
    	else
    	{
    		Iterator entries = vrGoodsCache.entrySet().iterator();  
        	while (entries.hasNext()) {  
        	    Map.Entry entry = (Map.Entry) entries.next();  
        	    //String key = (String)entry.getKey(); 
           	   	//tmp=(VrGoodsDataClass) entry.getValue();
        	    list.add((VrGoodsDataClass) entry.getValue());
        	} 
    		//list再排序下
        	
    	}
     	return list;
    }  
    
    public VrGoodsDataClass Load_VrGoods_ByListFirst()
    {
    	VrGoodsDataClass tmp=null;
    	List<VrGoodsDataClass> list=Load_All_VrGoodsList();
    	if(list.size()>0)
    	{
    		for(VrGoodsDataClass obj:list)
        	{
        		if(CacheClass.getIntValue(obj.state)==1)
        		{
        			tmp=obj;
        		}
        	}
    	}
    	return tmp;
    }   
    
    public VrGoodsDataClass Load_VrGoods_ByGoodsid(String vrgoodsid)
    {
    	VrGoodsDataClass tmp=vrGoodsCache.get(vrgoodsid);
    	if(tmp==null)
    	{
    		Load_All_VrGoodsList();
    		tmp=vrGoodsCache.get(vrgoodsid);
    	}
    	return tmp;
    } 
   
   //*************************************

    private void initProvider() {
        if (provider == null ) {
            try {
                provider =VrGoodsProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }

}
